﻿@{
    ViewBag.Title = "Hubs";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table id="marbleTable" class="stripe row-border order-column">
    <thead>
        <th>ID</th>
        <th>Name</th>
        <th>Address</th>
        <th>Frequency</th>
        <th>Note</th>
        <th>Active</th>
        <th>MacAddress</th>
        <th>IPAddress</th>
        <th>TCP Port</th>
        <th>Configure</th>
    </thead>
    @foreach (var item in ViewBag.hubDetails)
    {  
        <tr onchange="UpdatedList(this)">
            <td>
                <lable class="fixedCol1">@(item.Id == 0 ? "" : item.Id) </lable>
            </td>
            <td>
                <input class="fixedCol2" type="text" value="@item.Name" />
            </td>
            <td>
                <input type="text" value="@item.Address" />
            </td>
            <td>
                <input type="text" value="@item.Frequency" />
            </td>
            <td>
                <input type="text" value="@item.Note" />
            </td>
            <td>
                <input type="checkbox"  @(item.Active ? "checked='checked'" : "") value="@item.Active" />
            </td>
            <td>
                <input type="text" value="@item.MacAddress"/>
            </td>
            <td>
                <input type="text" value="@item.IPAddress"/>
            </td>
            <td>
                <input type="text" value="@item.TCPPort"/>
            </td>
            <td>
                <button onClick="ConfigureHub(@item.Id)">-</button>
            </td>
        </tr>  
    }
</table>
<div class="action">
    <button class="btn btn-primary btn-md" role="button" onclick="SaveHubs()">Save</button>
    <button class="btn btn-primary btn-md" role="button" id="addRow">Add</button>
    <button class="btn btn-primary btn-md" role="button" onclick="history.back();" id="back">Back</button>
    <button class="btn btn-primary btn-md" role="button" onclick="location.href='/Marble/index'">Close</button>

</div>
@*<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>*@
<script>
    $(document).ready(function () {
        $('.dropdown-toggle').collapse({
            toggle: false
        });
    })
    $(document).ready(function () {
        var table = $('#marbleTable').DataTable({
            scrollY: "400px",
            scrollX: true,
            scrollCollapse: true,
            editable: true,
            stateSave: true,
            // paging: true,
            bPaginate: false,
            bInfo: false,
            bFilter: false,
            bSort: false,
            "createdRow": function (row, data, index) {
                $('td', row).addClass('tblRow');
            },
            rowCallback: function (row, data) {
                var element = $('td', row);
                element.on("change", function (e) {
                    UpdatedList($(this).parent().index());
                });
            },
            //order: [[ 0, "desc" ]],
            fixedColumns: {
                leftColumns: 2
            }
        });
        var counter = 1;

        $('#addRow').on('click', function () {
            var fixedcolumn1values = $('.DTFC_Cloned lable.fixedCol1');
            var fixedcolumn2values = $('.DTFC_Cloned input.fixedCol2');
           
            table.row.add([
                '<lable class="fixedCol1"></lable>',
                '<input class="fixedCol2" type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="checkbox" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<button disabled>-</button>',
            ]).draw(false);

            for (var i = 0, j = fixedcolumn1values.length; i < j; i++) {
                $('.DTFC_Cloned lable.fixedCol1')[i].innerHTML = fixedcolumn1values[i].innerHTML;
            }
            for (var i = 0, j = fixedcolumn2values.length; i < j; i++) {
                $('.DTFC_Cloned input.fixedCol2')[i].value = fixedcolumn2values[i].value;
            }
        });
         $(".tblRow").on("change", function (e) { UpdatedList($(this).parent().index()); });
    });

    
    function SaveHubs() {
        if (updatedArray.length > 0) {
            var hubs = [];
            var table = document.getElementById('marbleTable');
            var fixedcolumn1 = $('.DTFC_Cloned lable.fixedCol1');
            var fixedcolumn2 = $('.DTFC_Cloned input.fixedCol2');

            for (var r = 0, n = table.rows.length; r < n; r++) {
                if (updatedArray.includes(r)) {
                    var rowIndex = r + 1;
                    hubs.push({
                        Id: fixedcolumn1[r].innerHTML,
                        Name: fixedcolumn2[r].value,
                        Address: table.rows[rowIndex].cells[2].children[0].value,
                        Frequency: table.rows[rowIndex].cells[3].children[0].value,
                        Note: table.rows[rowIndex].cells[4].children[0].value,
                        Active: table.rows[rowIndex].cells[5].children[0].checked,
                        MacAddress: table.rows[rowIndex].cells[6].children[0].value,
                        IPAddress: table.rows[rowIndex].cells[7].children[0].value,
                        TCPPort: table.rows[rowIndex].cells[8].children[0].value,
                    });
                }
            }
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: '@Url.Action("UpdateHubs", "Game")',
                data: JSON.stringify({ hubs: hubs }),
                dataType: "json",
                success: function (data) {
                    alert('Data Saved');
                    location.reload();
                },
                error: function (e) {
                    alert(e);
                }
            });
        }
    }
    var updatedArray = [];
    function UpdatedList(index) {
        if (!updatedArray.includes(index)) {
            updatedArray.push(index);
        }
    }
    function ConfigureHub() {
    }
</script>
